package ModelLayer;
import java.util.ArrayList;

/**
 * Class for objects of PersonCollection
 * 
 * @author (AF, RA, KP, RL) 
 * @version (2012.06.05)
 */
public class PersonCollection
{
    private static PersonCollection instance;
    private ArrayList<Person> persons;

    /**
     * Constructor for objects of class PersonCollection
     */
    private PersonCollection()
    {
        persons = new ArrayList<Person>();
    }
    
    /**
     * implementation of singleton pattern
     * 
     * @param none
     * 
     * @return instance of object of type PersonCollection only if it does not exist
     */
    public static PersonCollection getInstance()
    {
        if (instance == null) {
            instance = new PersonCollection();
        }
        
        return instance;    
    }

    /**
     * add object of type Person to PersonCollection
     * 
     * @param person person to add to PersonCollection as Person
     * 
     * @return none
     */
    public void addPerson(Person person)
    {
        persons.add(person);
    }
    
    /**
     * get object of type Person from phoneNo
     * 
     * @param phoneNo phoneNo of person as String
     * 
     * @return object person of type Person
     */
    public Person findPerson(String phoneNo) 
    {
        Person person = null;
        int index = 0;
        boolean found = false;
        while(index < persons.size() && !found)
        {
            person = persons.get(index);
            if(person.getPhoneNo().equals(phoneNo))
            {
                found = true;
            }
            else{
                index++;
            }
        }
        
        return person;
    }
    
    /**
     * delete object person of type Person from phoneNo
     * 
     * @param phoneNo phoneNo of person as String
     * 
     * @return none
     */
    public boolean deletePerson(String phoneNo) 
    {
        Person person = null;
        int index = 0;
        boolean found = false;
        while(index < persons.size() && !found)
        {
            person = persons.get(index); 
            if(person.getPhoneNo().equals(phoneNo))
            {
                found = true;
                persons.remove(index);
            }
            else{
                index++;
            }
        }
        
        return found;
    }
    
    /**
     * get personCollection as ArrayList
     * 
     * @return all persons as ArrayList
     */
    public ArrayList<Person> listAllPersons()
    {
        return persons;
    }
}
